<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/08/29
 * Time: 08:51
 */

namespace Modules\Shop\Http\Controllers;

use Illuminate\Support\Facades\Request;
use Modules\Shop\Service\GoodsService;

class GoodsController extends BaseController
{
    /**
     * 商品列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $arr_option = Request::input();
        $arr_option['shop'] = shop_shop_id();//门店id

        $res = GoodsService::get_list($arr_option);

        echo_error($res);
        return view('shop::goods.index', ['arr_data' => $res['data'], 'search' => $arr_option]);
    }

    /**
     * 添加商品页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $cate = GoodsService::get_cate_list();//商城分类
        $specs = GoodsService::get_specs_cate_list(shop_shop_id());//规格属性列表
        $maintain = GoodsService::get_maintain_config();// 保养配置
        $sale_tag = GoodsService:: get_sale_tag();//商品服务标签

        $result = [
            'cate' => $cate,
            'specs' => $specs,
            'maintain' => $maintain['data'],
            'sale_tag' => $sale_tag
        ];
        return view('shop::goods.create', $result);
    }

    /**
     * 根据句条件获取服务的二级分类
     * @return false|string
     */
    public function get_maintain_list()
    {
        $input = Request::all();
        $pid = intval($input['pid']);

        $list = GoodsService::get_maintain_config($pid);

        return json_encode($list);
    }

    /**
     * 添加 商品 操作
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function store()
    {
        $arr_input = Request::input();
        $arr_input['goods']['repairstore_id'] = shop_shop_id();//门店id

        shiwu_start();
        $res = GoodsService::add_one($arr_input);
        shiwu_end($res);

        return $res;
    }

    /**
     * 修改 商品 页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit()
    {
        $cate = GoodsService::get_cate_list();//商品分类
        $specs = GoodsService::get_specs_cate_list(shop_shop_id());//规格属性列表
        $sale_tag = GoodsService:: get_sale_tag();//商品服务标签

        $nId = Request::get('id', 0);
        $res = GoodsService::get_one((int)$nId);

        $maintain = GoodsService::get_maintain_config();// 保养配置(父级)
        $son = GoodsService::get_maintain_config(intval($res['data']['maintain_pid']));// 保养配置（子级）

        $arr_data = [
            'arr_data' => $res['data'],
            'cate' => $res['data']['type'] == 2 ? $cate['integral'] : $cate['ordinary'],
            'specs' => $specs,
            'sale_tag' => $sale_tag,
            'maintain' => $maintain['data'],
            'son' => $son['data']
        ];

        return view('shop::goods.edit', $arr_data);
    }

    /**
     * 修改 商品 操作
     * @return array|\Illuminate\Http\RedirectResponse
     * @throws \Exception
     */
    public function update()
    {
        $arr_input = Request::input();

        shiwu_start();
        $res = GoodsService::edit_one($arr_input);
        shiwu_end($res);

        return $res;
    }


    /**
     * 上下架
     * @return array
     */
    public function changeState()
    {
        $arr_input = Request::input();

        return GoodsService::change_one_state($arr_input);
    }

    /**
     * 删除 商品
     * @return array|\Illuminate\Http\RedirectResponse
     */
    public function del()
    {
        $n_id = (int)Request::get('id', 0);
        return GoodsService::delete_one($n_id);
    }

    /**
     * 删除 商品图片
     * @return array
     */
    public function delPic()
    {
        $id = (int)Request::post('id', 0);
        return GoodsService::delPic((int)$id);
    }

    /**
     * 删除 商品规格属性
     * @return array
     */
    public function delProduct()
    {
        $id = (int)Request::post('id', 0);
        return GoodsService::delProduct((int)$id);
    }
}